import router from "./index"
import useStore from '@/store';
router.beforeEach(async (to, from, next) => {
    const store = useStore();
    // 已经登录
    if (sessionStorage.getItem('token')) {
        if (store.user.dynamicRouter) {
            next()
        } else {
            try {
                await store.user.getDynamicRouter()
                next({ path: to.path })
            } catch (error) {
                console.error(error)
                store.user.logout()
            }
        }
    } else {
        // 防止一直重定向
        if (to.fullPath === "/login") {
            next()
        } else {
            next({
                path: "/login"
            })
        }
    }
})